package com.teamc.team.controller;

import com.teamc.team.entity.Link;
import com.teamc.team.response.ResponseData;
import com.teamc.team.service.ILinkService;
import com.teamc.team.util.ResponseDataUtil;
import com.teamc.team.vo.AdminOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author ding
 */
@Controller
public class AdminOrderController {

    private final ILinkService linkService;

    @Autowired
    public AdminOrderController(ILinkService linkService) {
        this.linkService = linkService;
    }

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @GetMapping("/admin/findAllOrderByUserID/{id}")
    @ResponseBody
    public ResponseData findByUserId(@PathVariable("id") String id) {
        return ResponseDataUtil.buildSuccess(linkService.listAllByName(id));
    }

    /**
     * 查询所有归还了的
     * @return
     */
    @GetMapping("/admin/findAllOrderByReturned/")
    @ResponseBody
    public ResponseData findAllReturnedUser() {
        return ResponseDataUtil.buildSuccess(linkService.listAllReturned());
    }

    /**
     * 查询所有未归还的
     * @return
     */
    @GetMapping("/admin/findAllOrderByNotReturned/")
    @ResponseBody
    public ResponseData findAllNotReturnedUser() {
        return ResponseDataUtil.buildSuccess(linkService.listAllUnReturned());
    }

    /**
     * 通过用户id删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/admin/deleteOrderByUserID/{id}")
    @ResponseBody
    public ResponseData deleteOrderByUserId(@PathVariable("id") String id) {
        try {
            linkService.deleteLinkByName(id);
            return ResponseDataUtil.buildSuccess("删除成功");
        } catch (Exception e) {
            return ResponseDataUtil.buildError("删除失败");
        }
    }

    /**
     * 通过订单id删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/admin/deleteOrderByLinkID/{id}")
    @ResponseBody
    public ResponseData deleteOrderByLinkId(@PathVariable("id") Integer id) {
        try {
            linkService.deleteLinkByLinkId(id);
            return ResponseDataUtil.buildSuccess("删除成功");
        } catch (Exception e) {
            return ResponseDataUtil.buildError("删除失败");
        }
    }

    /**
     * 归还方法
     * @param id
     * @return
     */
    @PostMapping("/admin/returnObjectByID/{id}")
    @ResponseBody
    public ResponseData returnObject(@PathVariable("id") Integer id) {
        try {
            linkService.returnObject(id);
            return ResponseDataUtil.buildSuccess("归还成功");
        } catch (Exception e) {
            return ResponseDataUtil.buildError("归还失败");
        }
    }

    @RequestMapping("/admin/order")
    public String order(Model mo) {
        List<AdminOrderVO> adminOrderVOS = linkService.listAllLink();
        mo.addAttribute("list",adminOrderVOS);
        return "admin/records";
    }
}
